﻿
/*//链表节点
public class Node<T> {
    public Node<T> Next { get; set; }
    public T Data { get; set; }
    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

//泛型链表类
public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;
    public GenericList()
    {
        tail = head = null;
    }
    public Node<T> Head
    {
        get => head;
    }
    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        //整形List
        GenericList<int> intList = new GenericList<int>();
        for(int x = 0; x < 10; x++)
        {
            intList.Add(x);
        }
        for (Node<int> node = intList.Head;node!=null;node=node.Next)
        {
            Console.WriteLine(node.Data);
        }
        GenericList<string> strList = new GenericList<string>();
        for(int x = 0; x < 10; x++)
        {
            strList.Add("str" + x);
        }
        for (Node<string> node = strList.Head; node != null; node = node.Next)
        {
            Console.WriteLine(node.Data);
        }
    }
}*/

using System;

public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }
    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList()
    {
        tail = head = null;
    }

    public Node<T> Head
    {
        get => head;
    }

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    // 添加ForEach方法
    public void ForEach(Action<T> action)
    {
        for (Node<T> node = head; node != null; node = node.Next)
        {
            action(node.Data);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 整形链表
        GenericList<int> intList = new GenericList<int>();
        for (int x = 0; x < 10; x++)
        {
            intList.Add(x);
        }

        // 使用 ForEach 打印链表元素
        Console.WriteLine("打印整形链表元素：");
        intList.ForEach(x => Console.WriteLine(x));

        // 求整形链表的最大值
        int max = int.MinValue;
        intList.ForEach(x => {
            if (x > max) max = x;
        });
        Console.WriteLine($"整形链表的最大值是：{max}");

        // 求整形链表的最小值
        int min = int.MaxValue;
        intList.ForEach(x => {
            if (x < min) min = x;
        });
        Console.WriteLine($"整形链表的最小值是：{min}");

        // 求整形链表的求和
        int sum = 0;
        intList.ForEach(x => sum += x);
        Console.WriteLine($"整形链表的总和是：{sum}");

        // 字符串链表
        GenericList<string> strList = new GenericList<string>();
        for (int x = 0; x < 10; x++)
        {
            strList.Add("str" + x);
        }

        // 使用 ForEach 打印字符串链表元素
        Console.WriteLine("打印字符串链表元素：");
        strList.ForEach(x => Console.WriteLine(x));
    }
}